/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bagtest;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author haoliang.donghl
 */
public class BagTest {
    private final int TEST_TIMES = 1000;
    private final int ITEM_COUNT = 100;
    private float capacity;
    private ArrayList<Item> items;
    private ArrayList<Item> inBag;

    public static void main(String[] args) {
        BagTest instance = new BagTest();
        instance.go();
    }

    private void go(){
        AbstractAlgorithm algo1 = new DynamicAlgorithm();
        AbstractAlgorithm algo2 = new GreedyAlgorithm();
        inBag = new ArrayList<Item>();
        items = new ArrayList<Item>();

        int sbig = 0, ssmall = 0, sequal = 0;
        float result1, result2;
        for (int i = 0; i < TEST_TIMES; i++){
            //System.out.println("=============" + (i + 1) + "===============");

            //System.out.println("ALL VALUE:\t" + generateData());
            generateData();
            algo1.setData(capacity, items, inBag);
            algo1.doCalculate();
            result1 = algo1.getValue();
            //algo1.printResult();
            System.out.print(result1 + ",");
            takeOutAll();

            algo2.setData(capacity, items, inBag);
            algo2.doCalculate();
            result2 = algo2.getValue();
            //algo2.printResult();
            System.out.println(result2);
            takeOutAll();

            if (result1 > result2){
                sbig++;
            }else if(result1 < result2){
                ssmall++;
            }else{
                sequal++;
            }
            
            //System.out.println("=================================");
        }

        System.out.println("Dynamic > Greedy: \t" + sbig);
        System.out.println("Dynamic < Greedy: \t" + ssmall);
        System.out.println("Dynamic = Greedy: \t" + sequal);
    }

    private float generateData(){
        Random random = new Random();
        float value = 0;

        inBag.clear();
        items.clear();
        
        capacity = 1.0f;
        for (int i = 0; i < ITEM_COUNT; i++){
            Item item = new Item();
            item.setSize(random.nextFloat() * capacity * 8.0f / ITEM_COUNT
                       + capacity * 0.125f / ITEM_COUNT);
            item.setValue(random.nextFloat() * 10);
            items.add(item);
            value += item.getValue();
        }

        return value;
    }

    private void takeOutAll(){
        inBag.clear();
    }
}
